package com.tractorframework.core;

import java.lang.reflect.Array;

class StringToObjectConverter {

	public Object toObject(String expression, Class toClazz)
	{
		if(isPrimitive(toClazz))
		{
			return toPrimitive(expression, toClazz);
		}
		else if(toClazz.isArray())
		{
			String[] items = expression.split(",");
			return toArrays(items, toClazz);
		}
		return expression;
	}
	
 public  boolean isPrimitive(Class clz) 
 { 
        try 
        { 
        	if(clz.isPrimitive())
        	{
        		return true;
        	}
           return ((Class) clz.getField("TYPE").get(null)).isPrimitive();
        } 
        catch (Exception e) { 
            return false; 
        } 
    } 
	
	private Object toArrays(String[] items, Class arrayClazz)
	{
		int length = items.length;
		Class itemClazz = String.class;
		if(arrayClazz.equals(boolean[].class) || arrayClazz.equals(Boolean[].class))
		{
			itemClazz = boolean.class;
		}
		else if(arrayClazz.equals(byte[].class)|| arrayClazz.equals(Byte[].class))
		{
			itemClazz = byte.class;
		}
		else if(arrayClazz.equals(char[].class)|| arrayClazz.equals(Character[].class))
		{
			itemClazz = char.class;
		}
		else if(arrayClazz.equals(short[].class)|| arrayClazz.equals(Short[].class))
		{
			itemClazz = short.class;
		}
		else if(arrayClazz.equals(int[].class)|| arrayClazz.equals(Integer[].class))
		{
			itemClazz = int.class;
		}
		else if(arrayClazz.equals(long[].class)|| arrayClazz.equals(Long[].class))
		{
			itemClazz = long.class;
		}
		else if(arrayClazz.equals(float[].class)|| arrayClazz.equals(Float[].class))
		{
			itemClazz = float.class;
		}
		else if(arrayClazz.equals(double[].class)|| arrayClazz.equals(Double[].class))
		{
			itemClazz = double.class;
		}
		Object array = Array.newInstance(itemClazz, length);
		for(int i = 0; i < length; i++)
		{
			Array.set(array, i, toPrimitive(items[i], itemClazz));
		}
		return array;
	}
	
	
	private Object toPrimitive(String expression, Class primitiveClazz)
	{
		Object result = expression;
		if(primitiveClazz.equals(Boolean.class) || Boolean.TYPE == primitiveClazz)
		{
			result = Boolean.parseBoolean(expression);
		}
		else if(primitiveClazz.equals(Byte.class) || Byte.TYPE == primitiveClazz)
		{
			result = Byte.parseByte(expression);
		}
		else if(primitiveClazz.equals(Character.class) || Character.TYPE == primitiveClazz)
		{
			result = (char)Short.parseShort(expression);
		}
		else if(primitiveClazz.equals(Integer.class) || Integer.TYPE == primitiveClazz)
		{
			result = Integer.parseInt(expression);
		}
		else if(primitiveClazz.equals(Long.class) || Long.TYPE == primitiveClazz)
		{
			result = Long.parseLong(expression);
		}
		else if(primitiveClazz.equals(Short.class) || Short.TYPE == primitiveClazz)
		{
			result = Short.parseShort(expression);
		}
		else if(primitiveClazz.equals(Float.class) || Float.TYPE == primitiveClazz)
		{
			result = Float.parseFloat(expression);
		}
		else if(primitiveClazz.equals(Double.class) || Double.TYPE == primitiveClazz)
		{
			result = Double.parseDouble(expression);
		}
		return result;
	}
}
